Repository navigation
Unstructure subclassed built-in collections like their built-ins - #824
Closed
Aschenbath wants to merge 1 commit into
Closed
Aschenbath wants to merge 1 commit into
Aschenbath wants to merge 1 commit into
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #287.
Subclasses of built-in collections were not un/structured like their built-ins:
converter.unstructure(MyList([Item(1)]))returned the rawItemobjects, and a field annotatedMyList[Item]was passed through unchanged too, whilelist[Item]worked.Two gaps in the type predicates in
cattrs._compat:is_sequence,is_mutable_sequence,is_tuple,is_deque,is_mutable_set,is_frozenset,is_mapping) only recognizedtyping._GenericAlias. Subscripting a user class produces a PEP 585types.GenericAliasinstead, soMyList[Item]never matched. These predicates now accept both alias types (asis_genericalready did).is_mapping, the sequence and set predicates had no clause for bare subclasses, so dispatching on a bareMyListclass (for example when unstructuring an instance directly) fell through to the identity fallback.is_mutable_sequence,is_mutable_setandis_frozensetnow also match bare subclasses oflist/deque,setandfrozensetrespectively, mirroring the existingis_mappingclause.Class-registered hooks (for example for
str,bytesorPathsubclasses) still take precedence, since singledispatch runs before these predicates, and attrs classes are still matched earlier by thehaspredicate.Tests:
test_unstructure_subclassed_listandtest_unstructure_subclassed_setstotests/test_baseconverter.py, covering bare instances, bare and subscripted field annotations, and a structuring round-trip.tests/test_baseconverter.py,tests/test_gen_dict.py,tests/test_dicts.py,tests/test_function_dispatch.py,tests/test_multistrategy_dispatch.py,tests/test_tuples.py,tests/test_structure.py,tests/test_unstructure.py,tests/test_unstructure_collections.py,tests/test_cols.py,tests/test_defaultdicts.py,tests/test_typeddicts.py— all passed (257 tests).ruff checkandruff format --checkon the changed files: pass.A changelog entry is included in
HISTORY.md.